package com.tbm.impl.service;

import com.tbm.base.service.SystemUserService;
import com.tbm.entity.SystemUser;
import com.tbm.entity.UserEntity;
import com.tbm.repository.SystemUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */
@Service
public class SystemUserServiceImpl implements SystemUserService{

    @Autowired
    private SystemUserRepository systemUserRepository;


    @Override
    public SystemUser findByUserName(String userName) {
        return systemUserRepository.findByUserName(userName);
    }

    @Override
    public List<SystemUser> findAll() {
        return systemUserRepository.findAll();
    }

    @Override
    public List<UserEntity> findAllSystemUserAndRole() {
        List<Object> allSystemUserAndRole = systemUserRepository.findAllSystemUserAndRole();
        List<UserEntity> userEntityList = new ArrayList<>();
        for(Object o : allSystemUserAndRole){
            if(o instanceof Object[]){
                Object[] obj = (Object[])o;
                UserEntity userEntity = new UserEntity();
                if(obj.length<9){
                    continue;
                }else{
                    userEntity.setUserId(Integer.valueOf(obj[0].toString()));
                    userEntity.setUserName(obj[1]==null?"":obj[1].toString());
                    userEntity.setDisplayName(obj[2]==null?"":obj[2].toString());
                    userEntity.setIsLock(obj[3]==null?"":obj[3].toString());
                    userEntity.setPhone(obj[4]==null?"":obj[4].toString());
                    userEntity.setEmail(obj[5]==null?"":obj[5].toString());
                    userEntity.setRoleId(Integer.valueOf(obj[6].toString()));
                    userEntity.setRoleName(obj[7]==null?"":obj[7].toString());
                    userEntity.setRights(obj[8]==null?"":obj[8].toString());

                    userEntityList.add(userEntity);
                }

            }
        }
        return userEntityList;
    }

    @Override
    public Page<SystemUser> findAll(Pageable pageable) {
        return systemUserRepository.findAll(pageable);
    }

    @Override
    public int count() {
        return (int) systemUserRepository.count();
    }

    @Override
    public void delete(Integer id) {
        systemUserRepository.delete(id);
    }


    @Override
    public void save(SystemUser systemUser) {
        systemUserRepository.save(systemUser);
    }

    @Override
    public SystemUser findOne(Integer userId) {
        return systemUserRepository.findOne(userId);
    }

    @Override
    public Page<SystemUser> findAll(Example<SystemUser> var1, Pageable var2) {
        return systemUserRepository.findAll(var1,var2);
    }

    @Override
    public void saveAndFlush(SystemUser systemUser) {
        systemUserRepository.saveAndFlush(systemUser);
    }
}
